package com.zzjson.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Calendar;
import java.util.Date;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.quartz</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年04月03日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class HelloSchedule {
	public static void main(String[] args) throws SchedulerException {
		//jobdetail 绑定信息job
		JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).
				withIdentity("myJob", "group1").
				usingJobData("message", "helloMyJob1").
				usingJobData("helloJob", "wow").build();
		System.out.println(jobDetail.getKey());
		//创建一个Trigger实例，定义该Job立即执行，并且每隔两秒钟重复执行一次，知道永远
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, 10);

		Trigger trigger = TriggerBuilder.newTrigger().
				withIdentity("myTrigger", "group1").
				//当前时间开始
				//startNow()
				//开始时间
						startAt(new Date()).
				//结束时间
						endAt(calendar.getTime())
				.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()
				).usingJobData("myTrigger", "triggerString").usingJobData("helloTrigger", "wow").build();
		//创建Scheduler实例，通过factory的模式创建
		StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = stdSchedulerFactory.getScheduler();
		scheduler.scheduleJob(jobDetail, trigger);
		scheduler.start();
	}
}